import axios from 'axios'

import './__mock__'

/** 检查运单号是否存在 */
export const apiCheckWaybillExists = async (
  waybillNos: string[],
): Promise<{
  /** 结果 map */
  resultMap: Record<string, boolean | undefined>
  /** 有效运单数 */
  validNum: number
  /** 无效运单数 */
  invalidNum: number
}> => {
  return axios.post('/tmsapi/waybill_record/checkWaybillNumber', waybillNos).then(({ $data }) => {
    const res: boolean[] = $data || []
    const resultMap: Record<string, boolean | undefined> = {}
    let validNum = 0
    let invalidNum = 0
    res.forEach((v, i) => {
      resultMap[waybillNos[i]] = v
      if (v) validNum++
      else invalidNum++
    })
    return { resultMap, validNum, invalidNum }
  })
}

/** 保存运单附件 */
export const apiSaveWaybillFiles = async (
  req: Array<{
    /** 运单号 */
    waybillNo: string
    fileUrl: string
    fileName: string
    /** 文件类型：1.装箱单 2.报关文件 3.清关文件 4.标签文件 5.签收文件(POD) 6.其他文件 */
    fileType: number
  }>,
) => {
  await axios.post('/tmsapi/waybill_record/uploadWaybillAttach', { attachListParams: req })
}
